package v2.parser

/**
 * @author bweber
 * @version $Rev$, $Date$
 */

trait Slotted {
  
  // instance slots
  var slots:List[Slot]
  
  def listAttrs() = {
    var attrs = List[String]()
    for ( slot <- slots ) {
      attrs = slot.attrref :: attrs
    }
    attrs
  }
  
  def hasAttr(name:String):Boolean = {
    for ( slot <- slots ) {
      if ( slot.attrref == name ) {
        return true
      }
    }
    return false
  }
  
  def addAttr(name:String) {
    if ( hasAttr(name) ) {
      throw new RuntimeException("Slot " + name + " already exists.")
    }
    slots = Slot(name,null) :: slots
    this
  }

  def removeAttr(name:String) {
    for ( slot <- slots ) {
      if ( slot.attrref == name ) {
        slots = slots - slot
        return this
      }
    }
    throw new RuntimeException("No such attribute in single: " + name)
  }
  
  // ************************ instance specific stuff (has to do with 'value')
  
  def clearAttr(name:String) {
    for ( slot <- slots ) {
      if ( slot.attrref == name ) {
        slot.value = null
        return
      }
    }
    throw new RuntimeException("No such attribute: " + name)
  }
  
  // note: this is for adding instance specific attributes NOT on the type!
  def addAttr(name:String,value:Value) {
    if ( hasAttr(name) ) {
      throw new RuntimeException("Already has attribute: " + name)
    }
    slots = Slot(name,value) :: slots
    this
  }
  
  def setAttr(name:String,value:Value) {
    for ( slot <- slots ) {
      if ( slot.attrref == name ) {
        slot.value = value
        return
      }
    }
    throw new RuntimeException("No such attribute: " + name)
  }
  
  def getAttr(name:String):Value = {
    for ( slot <- slots ) {
      if ( slot.attrref == name ) {
        return slot.value
      }
    }
    throw new RuntimeException("No such attribute: " + name)
  }
}